package com.chy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chy.common.R;
import com.chy.common.Utils;
import com.chy.entity.Category;
import com.chy.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping("/list")
    public R<Page> getCategoryList(@RequestBody Map map) {
        Integer page = Utils.MapGetValue(map, "page");
        Integer pageSize = Utils.MapGetValue(map, "pageSize");
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Category::getSort);
        wrapper.orderByDesc(Category::getCreateTime);
        categoryService.page(pageInfo, wrapper);
        return R.success(pageInfo);
    }
    @PostMapping("/allList")
    public R<List<Category>> getAllCategoryList(@RequestBody Map<String, Object> params) {
        List<Category> list = categoryService.getAllCategoryList(params);
        return R.success(list);
    }

    @PostMapping("/add")
    public R<Category> addCategory(@RequestBody Category category) {
        return categoryService.save(category) ? R.success() : R.error();
    }
    @PostMapping("/update")
    public R<String> updateCategory(@RequestBody Category category) {
        return categoryService.updateById(category) ? R.success() : R.error();
    }

    @PostMapping("/delete")
    public R<String> deleteCategory(@RequestBody List<Category> list) {
        return categoryService.removeBatchByIds(list) ? R.success() : R.error();
    }

}
